﻿using System;
using Microsoft.Xna.Framework;

namespace MaterialEditor
{
    public class OutputNode : Node
    {
        private NodeValue _result;
        public override NodeValue Result
        {
            get { return difuseInput.value; }
        }

        /// <summary>
        /// the value of the diffuse map
        /// </summary>
        public InputSlot difuseInput
        {
            get { return input[0]; }
        }

        /// <summary>
        /// The value of the normal map
        /// </summary>
        public InputSlot normalInput
        {
            get { return input[1]; }
        }

        /// <summary>
        /// The value of the specular map
        /// </summary>
        public InputSlot specularInput
        {
            get { return input[2]; }
        }       
        
        private NodeValueType[][] _acceptTypes;
        public override NodeValueType[][] acceptTypes
        {
            get { return _acceptTypes; }
        }

        public OutputNode(int x, int y)
            :base(x, y)
        {
            input = new InputSlot[]
            {
                new InputSlot(this, null, "Diffuse Map"),
                new InputSlot(this, null, "Normal Map"),
                new InputSlot(this, null, "Specular Map"),
                new InputSlot(this, null, "Emmisive Map"),
            };

            _acceptTypes = new NodeValueType[][]
            {
                new NodeValueType[] { NodeValueType.ValueTexture },
                new NodeValueType[] { NodeValueType.ValueTexture },
                new NodeValueType[] { NodeValueType.ValueTexture },
            };

            Name = "Output";

            difuseInput.Connected += difuseInput_Connected;
            specularInput.Connected += specularInput_Connected;
            normalInput.Connected += normalInput_Connected;

            difuseInput.Disconnected += difuseInput_Disconnected;
        }

        void difuseInput_Disconnected()
        {
            difuseInput.value.TimeChanged -= UpdateTime;
        }

        void normalInput_Connected()
        {
            normalInput.value = normalInput.connector.startSlot.value;
            Form1.instance.renderPreviewWindow.cubeRender1.NormalMap = normalInput.value.AsTextureContainer().texture.Texture;
        }

        void difuseInput_Connected()
        {
            difuseInput.value = difuseInput.connector.startSlot.value;
            difuseInput.value.TimeChanged += UpdateTime;
            Form1.instance.renderPreviewWindow.cubeRender1.Texture = difuseInput.value.AsTextureContainer().texture.Texture;
        }

        void specularInput_Connected()
        {
            specularInput.value = specularInput.connector.startSlot.value;
        }

        private void UpdateTime()
        {
            if (difuseInput.value != null && difuseInput.value.AsTextureContainer() != null && difuseInput.value.AsTextureContainer() != null)
                Form1.instance.renderPreviewWindow.cubeRender1.Texture = difuseInput.value.AsTextureContainer().texture.Texture;
            if (normalInput.value != null && normalInput.value.AsTextureContainer() != null && normalInput.value.AsTextureContainer() != null)
                Form1.instance.renderPreviewWindow.cubeRender1.Texture = normalInput.value.AsTextureContainer().texture.Texture;
        }


        public override object Clone()
        {
            OutputNode n = new OutputNode((int)position.X, (int)position.Y);
            n._result = new NodeValue(_result);

            return n;
        }
    }
}